﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>

int my_strlen(const char* arr)
{
	int ret = 0;
	while (*arr++)
	{
		ret++;
	}
	return ret;
}

int my_strncmp(const char* arr1, const char* arr2, int sz)
{
	while ((*arr1 != '\0' || *arr2 != '\0') && sz)
	{
		if (*arr1 - *arr2)
		{
			return *arr1 - *arr2;
		}
		sz--;
		arr1++;
		arr2++;
	}
	return 0;
}

char* my_strstr(const char* arr1, const char* arr2)
{
	assert(arr1 && arr2);
	int len1 = my_strlen(arr1);
	int len2 = my_strlen(arr2);
	int i = 0;
	for (i = 0; i < len1 - len2 + 1; i++)
	{
		int cmp = my_strncmp(arr1 + i, arr2, len2);
		if (cmp == 0)
		{
			return (char*)arr1 + i;
		}
	}
	return NULL;
}

int main()
{
	char arr1[] = "zhengyuhang@qq.com";
	char arr2[] = "qq";
	char* arr3 = my_strstr(arr1, arr2);
	if (arr3 == NULL)
	{
		printf("查找的字符串不存在\n");
	}
	else
	{
		printf("%s\n", arr3);
	}
	return 0;
}